package com.wx.cloud.student.service.impl;

import com.wx.cloud.student.mapper.StudentMapper;
import com.wx.cloud.student.model.TransferTeam;
import com.wx.cloud.student.model.User;
import com.wx.cloud.student.service.StudentService;
import com.wx.cloud.student.utils.MyDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public User findUserByUserCode(String userCode) {
        return studentMapper.findUserByUserCode(userCode);
    }

    @Override
    public List<User> getUserByTeamCode(String teamCode) {
        return studentMapper.getUserByTeamCode(teamCode);
    }

    @Override
    public List<User> getUserItemByNoTeamCode() {
        return studentMapper.getUserItemByNoTeamCode();
    }

    @Override
    public String updateUserTeam(String teamCode, List<Integer> ids) {
        Map params = new HashMap();
        params.put("teamCode",teamCode);
        params.put("ids",ids);
        params.put("updateTime",MyDate.getDate());
        int result = studentMapper.updateUserTeam(params);
        if (result>0){
            return "success";
        }else {
            return "false";
        }
    }

    @Override
    public int getCountByTeam(String teamCode) {
        return studentMapper.getCountByTeam(teamCode);
    }

    @Override
    public String updateUserRole(String teamCode, String userCode) {
        User user = new User();
        user.setTeamCode(teamCode);
        user.setUserCode(userCode);
        user.setUpdateTime(MyDate.getDate());
        int result = studentMapper.updateUserToStu(user);
        int data = studentMapper.updateUserToLea(user);
        if (result > 0 && data > 0){
            return "success";
        } else {
            return "false";
        }
    }

    @Override
    public String updateLeaderToStu(Integer id) {
        int result = studentMapper.updateLeaderToStu(id);
        if (result > 0){
            return "success";
        } else {
            return "false";
        }
    }

    @Override
    public int delStudent(User user) {
        user.setUpdateTime(MyDate.getDate());
        return studentMapper.delStudent(user);
    }

    @Override
    public List<User> getStudentList(Map<String, Object> data) {
        int page = Integer.parseInt((String)data.get("pageNum"));
        int size = Integer.parseInt((String)data.get("pageSize"));
        data.put("pageNum",(page-1)*size);
        data.put("pageSize",size);
        return studentMapper.getStudentList(data);
    }

    @Override
    public int getStudentCount(Map<String, Object> data) {
        return studentMapper.getStudentCount(data);
    }

    @Override
    public int updateStudent(User user) {
        user.setUpdateTime(MyDate.getDate());
        User u = studentMapper.findUserByUserCode(user.getUserCode());
        int result = studentMapper.updateStudent(user);
        if (result > 0){
            String oldTeamCode = u.getTeamCode();
            String newTeamCode = user.getTeamCode();
            TransferTeam transferTeam = new TransferTeam();
            transferTeam.setUserCode(user.getUserCode());
            transferTeam.setOldTeamCode(oldTeamCode);
            transferTeam.setNewTeamCode(newTeamCode);
            transferTeam.setCreateTime(MyDate.getDate());
            studentMapper.addTransferTeam(transferTeam);
        }
        return result;
    }

    @Override
    public List<User> getTeacherList() {
        return studentMapper.getTeacherList();
    }

    @Override
    public List<User> getCoachList() {
        return studentMapper.getCoachList();
    }

    @Override
    public List<User> getUserCodes(List<Integer> ids) {
        Map params = new HashMap();
        params.put("ids",ids);
        return studentMapper.getUserCodes(params);
    }

    @Override
    public int addTransferTeams(Map maps) {
        return studentMapper.addTransferTeams(maps);
    }

}
